package com.csx.data.routing.multids;

import lombok.AllArgsConstructor;
import lombok.Getter;

import javax.sql.DataSource;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;

/**
 * <p> what does this class do ? </p>
 *      数据源栈
 * <p> How does it work ?</p>
 * <p> Why does it do this ?</p>
 *
 * @author cuisongxu
 * @date 2023/10/20 周五 16:06
 */
@AllArgsConstructor
public class DataSourceStack {

    /**
     * 数据源栈持有器
     */
    private final ThreadLocal<Deque<String>> stackHolder = ThreadLocal.withInitial(LinkedList::new);

    @Getter
    private volatile String defaultDataSource;

    /**
     * 替换默认数据源
     * @param defaultDataSource 默认数据源
     * @return 默认数据源
     */
    public synchronized String replaceDefaultDataSource(String defaultDataSource) {
        String old = this.defaultDataSource;
        this.defaultDataSource = defaultDataSource;
        return old;
    }

    public void push(String dataSource) {
        Objects.requireNonNull(dataSource, "入栈的数据源不能为空");
    }

}
